import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:kq_flutter_core_widget/widgets/image/kq_image.dart';
import 'package:kq_flutter_core_widget/widgets/imageBox/kq_image_box.dart';
import 'package:kq_flutter_tv_widgets/resources/kq_theme_colors.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';

import '../../config/kq_global.dart';
import '../../resources/kq_images.dart';
import '../../theme/kq_theme_manager.dart';

class KqHeadImageView extends StatefulWidget {
  const KqHeadImageView(
      {super.key,
      this.iconSize = 80,
      required this.defaultUrl,
      this.title = '上传头像',
      this.backgroundUrl,
      this.netUrl,
      this.customWidget,
      this.gradient,
      this.space = 10,
      this.isCircle = false,
      this.height = 200,
      this.txtColor = KqThemeColors.text333,
      this.didSelected,
      this.didDelete});
  //展示文本
  final String title;
  //背景图片
  final String? backgroundUrl;
  //默认展示的
  final String defaultUrl;
  //上传了的图片  netUrl imageType针对网络上的
  final ImageUrl? netUrl;
  //上传了图片的widget
  final Widget? customWidget;
  //图片大小 默认图的
  final double iconSize;
  //上传的图片和文本展示之间的间隔
  final double space;
  //文本颜色
  final Color txtColor;
  //是否需要切割成圆角 默认图
  final bool isCircle;
  //高度
  final double height;
  //背景色
  final Gradient? gradient;
  //上传图片
  final Function? didSelected;
  //删除 右上角的删除
  final Function? didDelete;

  @override
  State<KqHeadImageView> createState() => _KqHeadImageViewState();
}

class _KqHeadImageViewState extends State<KqHeadImageView> {
  final ImagePicker _picker = ImagePicker();

  //从相册选择
  handleGallery() async {
    var pickImage = await _picker.pickImage(source: ImageSource.gallery);
    return pickImage;
  }

  //拍照
  handleTakePhoto() async {
    var pickImage = await _picker.pickImage(source: ImageSource.camera);
    return pickImage;
  }

  //展示网络上获取的
  Widget renderNetwork() {
    return ConstrainedBox(
      constraints: const BoxConstraints.expand(),
      child: Stack(
        children: [
          KqInkWell(
              onTap: () {
                //放大图片
                Get.toNamed('/ImgPreview', arguments: [
                  ImageUrl(
                      imageType: widget.netUrl!.imageType,
                      url: widget.netUrl!.url)
                ], parameters: {
                  'position': '0'
                });
              },
              child: Container(
                  color: KqThemeColors.bgF4,
                  height: widget.height,
                  alignment: Alignment.center,
                  child: widget.customWidget != null
                      ? widget.customWidget!
                      : KqImage.custom(
                          imageType: widget.netUrl!.imageType,
                          fit: BoxFit.fitHeight,
                          url: widget.netUrl!.url,
                          width: KqScreenUtil().screenWidth,
                          height: widget.height,
                        ))),
          Positioned(
              right: -1, //偏移点，要不然会有边
              top: -1,
              child: InkWell(
                  onTap: () {
                    //删除
                    if (widget.didDelete != null) {
                      widget.didDelete!();
                    }
                  },
                  child: SizedBox(
                    width: 40.r,
                    height: 40.r,
                    child: KqImage.assets(
                      fit: BoxFit.contain,
                      url: KqImages.commonIcDelete,
                      package: KqGlobal.packageName,
                    ),
                  )))
        ],
      ),
    );
  }

  //展示默认的图片
  Widget renderNormal() {
    return ConstrainedBox(
      constraints: const BoxConstraints.expand(),
      child: KqInkWell(
          onTap: () {
            //点击上传
            if (widget.didSelected != null) {
              widget.didSelected!();
            }
          },
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: [
              KqImage.assets(
                url: widget.defaultUrl,
                fit: BoxFit.contain,
                width: widget.iconSize,
                height: widget.iconSize,
                radius: widget.isCircle ? widget.iconSize / 2.0 : 0,
              ),
              Container(
                padding: EdgeInsets.only(top: widget.space),
                child: Text(
                  widget.title,
                  style: TextStyle(
                      color: widget.txtColor,
                      fontSize: KqThemeManager.instance
                              .getConfig()
                              .formConfig
                              .titleFontSize ??
                          16.sp),
                ),
              ),
            ],
          )),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget.height,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: KqThemeColors.bgF4,
        gradient: widget.gradient,
        image: widget.backgroundUrl != null
            ? DecorationImage(
                fit: BoxFit.cover, image: AssetImage(widget.backgroundUrl!))
            : null,
      ),
      child: widget.customWidget != null || widget.netUrl != null
          ? renderNetwork()
          : renderNormal(),
    );
  }
}
